package cn.burnit.view;


import cn.burnit.pojo.Reader;
import cn.burnit.service.ReaderService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 完善个人信息
 */
public class PersonInfoForm extends JFrame {

    private JLabel title,readerName,readerType,sex,max_num,days_num;
    private JTextField readerNameField,readerTypeField,sexField,max_numField,days_numField;
    private JButton cancel,edit,close;

    public PersonInfoForm() {

        Container container = getContentPane();
        container.setLayout(null);


        edit=new JButton("编辑");
        edit.setBounds(150,310,60,20);
        cancel=new JButton("取消");
        cancel.setBounds(220,310,60,20);
        close=new JButton("关闭");
        close.setBounds(220,310,60,20);

        //cancel.setBounds(220,310,60,20);
        close.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

        edit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                changeForm(e);
            }
        });



        title=new JLabel("个人信息");
        title.setFont(new Font("宋体", Font.PLAIN, 24));
        title.setBounds(170,30,200,25);
        readerName=new JLabel("读者名称:");
        readerName.setFont(new Font("宋体", Font.PLAIN, 16));
        readerName.setBounds(130,80,100,20);
        readerType=new JLabel("读者类型:");
        readerType.setFont(new Font("宋体", Font.PLAIN, 16));
        readerType.setBounds(130,120,100,20);
        sex=new JLabel("性别:");
        sex.setFont(new Font("宋体", Font.PLAIN, 16));
        sex.setBounds(162,160,100,20);
        max_num=new JLabel("最大可借数量:");
        max_num.setFont(new Font("宋体", Font.PLAIN, 16));
        max_num.setBounds(100,200,150,20);
        days_num=new JLabel("最大可借天数:");
        days_num.setFont(new Font("宋体", Font.PLAIN, 16));
        days_num.setBounds(100,240,150,20);



        readerNameField=new JTextField();
        readerNameField.setEditable(false);
        readerNameField.setBounds(220,80,130,20);
        readerTypeField=new JTextField();
        readerTypeField.setEditable(false);
        readerTypeField.setBounds(220,120,130,20);
        sexField=new JTextField();
        sexField.setEditable(false);
        sexField.setBounds(220,160,130,20);
        max_numField=new JTextField();
        max_numField.setEditable(false);
        max_numField.setBounds(220,200,130,20);
        days_numField=new JTextField();
        days_numField.setEditable(false);
        days_numField.setBounds(220,240,130,20);

        container.add(title);
        container.add(readerName);container.add(readerNameField);
        container.add(readerType);container.add(readerTypeField);
        container.add(sex);container.add(sexField);
        container.add(max_num);container.add(max_numField);
        container.add(days_num);container.add(days_numField);
        container.add(edit);container.add(close);container.add(cancel);
        cancel.setVisible(false);


        setTitle("个人信息");
        setSize(450,450);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
        //setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    private void changeForm(ActionEvent e) {
        this.readerNameField.setEditable(true);
        this.readerTypeField.setEditable(true);
        this.sexField.setEditable(true);
        this.edit.setText("保存");

        close.setVisible(false);
        cancel.setVisible(true);
        this.edit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                updateReader(e);
            }
        });

        this.cancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                readerNameField.setEditable(false);
                readerTypeField.setEditable(false);
                sexField.setEditable(false);
                edit.setText("编辑");
                close.setVisible(true);
            }
        });

        close.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
    }

    private void updateReader(ActionEvent e) {
        String readername = this.readerNameField.getText();
        String readertype = this.readerTypeField.getText();
        String sex = this.sexField.getText();
        ReaderService readerService = new ReaderService();
        Reader reader = new Reader(readername,readertype,sex);
        int i=readerService.addReader(reader);
        if (i>0){
            JOptionPane.showMessageDialog(null,"保存成功");
            Reader getReader=readerService.findReaderByReadername(reader.getReadername());
            if (getReader!=null){
                readerNameField.setText(getReader.getReadername());
                readerTypeField.setText(getReader.getReadertype());
                sexField.setText(getReader.getSex());
                max_numField.setText(String.valueOf(getReader.getMax_num()));
                days_numField.setText(String.valueOf(getReader.getDays_num()));
            }
            edit.setText("编辑");
            cancel.setVisible(false);
            close.setVisible(true);
            //dispose();

        }else {
            JOptionPane.showMessageDialog(null,"保存失败");
        }
    }

//    public static void main(String[] args) {
//        new PersonInfoForm();
//    }


}
